

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="记录自己在调试IIC遇到的问题">
  <meta name="author" content="liuwei">
  <meta name="keywords" content="">
  <meta name="description" content="记录自己在调试IIC遇到的问题">
<meta property="og:type" content="article">
<meta property="og:title" content="IIC 调试记录">
<meta property="og:url" content="https://www.llsll.xyz/2022/04/09/iic/index.html">
<meta property="og:site_name" content="LW&#39;s blog">
<meta property="og:description" content="记录自己在调试IIC遇到的问题">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/blackerrr/photos/raw/master/photo/stm32%E8%BE%93%E5%87%BA%E9%85%8D%E7%BD%AE.jpg">
<meta property="article:published_time" content="2022-04-09T10:43:09.531Z">
<meta property="article:modified_time" content="2022-04-08T12:11:34.964Z">
<meta property="article:author" content="liuwei">
<meta property="article:tag" content="学习记录">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/blackerrr/photos/raw/master/photo/stm32%E8%BE%93%E5%87%BA%E9%85%8D%E7%BD%AE.jpg">
  
  <title>IIC 调试记录 - LW&#39;s blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.llsll.xyz","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":null,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"QsGrK96fVoTEvV5jcXID1kB4-gzGzoHsz","app_key":"Bb0EjzO9Ya2umqFfV2tep53O","server_url":"https://qsgrk96f.lc-cn-n1-shared.com","path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>LW</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="IIC 调试记录">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-04-09 18:43" pubdate>
        2022年4月9日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      8.3k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      26 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">IIC 调试记录</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2022年4月8日 晚上
                
              </p>
            
            <div class="markdown-body">
              <h1
id="记录一下自己在调试iic的时候遇到的问题">记录一下自己在调试IIC的时候遇到的问题</h1>
<p><strong>切记，不要把不同的IIC底层代码混用，每一个模拟IIC都是一个整体，内部的函数之间都是有联系的，如果混用之后,可能会导致IIC通信出现问题。</strong></p>
<p>[TOC]</p>
<h2 id="拿来主义">拿来主义</h2>
<h2 id="iic基本概念">IIC基本概念</h2>
<ul>
<li>起始信号</li>
</ul>
<p>当SCL为高电平时，SDA一个下降沿</p>
<ul>
<li>停止信号</li>
</ul>
<p>当SCL为高电平时，SDA一个上升沿</p>
<ul>
<li>应答</li>
</ul>
<p>低电平表示应答</p>
<ul>
<li>非应答</li>
</ul>
<p>高电平表示非应答</p>
<ul>
<li>SCL和SDA都是高电平</li>
</ul>
<p>总线空闲</p>
<ul>
<li>IIC 读和写</li>
</ul>
<p><strong>0</strong>表示写控制， <strong>1</strong>表示读控制</p>
<p>写数据发送 addr &lt;&lt; 1 | 0, 读数据发送 addr &lt;&lt; 1 | 1</p>
<h2 id="iic-读取mpu6050">IIC 读取MPU6050</h2>
<p>时间 2022 0401</p>
<p>想试试用推挽和开漏两种模式调试IIC，但在操作过程中两次将MPU线子插反，导致读出来的数据跳变严重，将代码版本回退到昨天之后发现陀螺仪正常，感谢git,</p>
<ul>
<li>IIC 使用推挽输出</li>
</ul>
<ol type="1">
<li>正点原子的IIC代码用的是推挽输出，然后需要读数据的时候就设置SDA为输入模式，需要写数据的时候就设置SDA为输出模式。</li>
<li>经过实测，使用寄存器切换和库切换都是可以的，当时寄存器切换更好，速度比较快，</li>
</ol>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">IIC_SDA_OUT</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">// 使用寄存器操作比较迅速</span><br>    GPIOC-&gt;CRL &amp;= <span class="hljs-number">0XFFFFFF0F</span>;<br>    GPIOC-&gt;CRL |= <span class="hljs-number">3</span> &lt;&lt; <span class="hljs-number">4</span>;<br><br>    <span class="hljs-comment">// 使用库切换的话时间肯定比上面的慢， 但也是可以用的，优先选择使用寄存器切换</span><br>    <span class="hljs-comment">// GPIO_InitTypeDef GPIO_Initure;</span><br>    <span class="hljs-comment">// GPIO_Initure.Pin = GPIO_PIN_1;</span><br>    <span class="hljs-comment">// GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;</span><br>    <span class="hljs-comment">// GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;</span><br>    <span class="hljs-comment">// HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">IIC_SDA_IN</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">// 使用寄存器操作比较迅速</span><br>    GPIOC-&gt;CRL &amp;= <span class="hljs-number">0XFFFFFF0F</span>;<br>    GPIOC-&gt;CRL |= <span class="hljs-number">8</span> &lt;&lt; <span class="hljs-number">4</span>;<br><br>    <span class="hljs-comment">// 使用库切换的话时间肯定比上面的慢， 但也是可以用的，优先选择使用寄存器切换</span><br>    <span class="hljs-comment">// GPIO_InitTypeDef GPIO_Initure;</span><br>    <span class="hljs-comment">// GPIO_Initure.Pin = GPIO_PIN_1;</span><br>    <span class="hljs-comment">// GPIO_Initure.Mode = GPIO_MODE_INPUT;</span><br>    <span class="hljs-comment">// GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;</span><br>    <span class="hljs-comment">// HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);</span><br>&#125;<br></code></pre></td></tr></table></figure>
<ul>
<li><p>IIC 使用开漏输出</p>
<p><img src="https://gitee.com/blackerrr/photos/raw/master/photo/stm32%E8%BE%93%E5%87%BA%E9%85%8D%E7%BD%AE.jpg" srcset="/img/loading.gif" lazyload alt="STM32F1输出配置" style="zoom:80%;" /></p>
<ol type="1">
<li>如上图。开漏模式下，可以直接访问输入数据寄存器。所以就不用进行IO输入输出切换了。</li>
<li>代码中一般不需要上拉，外部一般都会有上拉电阻。（调试的经验比较少，暂时是这样）</li>
</ol>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs objectivec"><span class="hljs-keyword">void</span> <span class="hljs-built_in">MPU_IIC_Init</span>(<span class="hljs-keyword">void</span>)<br>&#123;<br>    GPIO_InitTypeDef GPIO_Initure;<br><br>    __HAL_RCC_GPIOC_CLK_ENABLE();<br>    GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1;<br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP    <span class="hljs-comment">// 使用推挽输出</span></span><br>    GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;<br>    <span class="hljs-comment">// GPIO_Initure.Pull = GPIO_PULLUP;     // 一般IIC设备SCL和SDA 接上拉电阻</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span><br>    GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;    <span class="hljs-comment">// 开漏输出模式</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;<br><br>    HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);<br><br>    <span class="hljs-built_in">MPU_IIC_SCL</span> = <span class="hljs-number">1</span>;<br>    <span class="hljs-built_in">MPU_IIC_SDA</span> = <span class="hljs-number">1</span>;    <span class="hljs-comment">// 总线空闲状态</span><br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>将一个IIC推挽模式的代码改成开漏模式的，不是仅仅将引脚推挽改成引脚开漏，去掉输入输出切换那么简单（实测）</p></li>
</ul>
<h2 id="我的iic代码-宏定义切换推挽模式还是开漏模式">我的IIC代码
（宏定义切换推挽模式还是开漏模式）</h2>
<ul>
<li><p>代码中涉及到的延时和位带操作参考正点原子的STM32代码</p></li>
<li><p>iic.c</p></li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">/*</span><br><span class="hljs-comment"> * @Date         : 2022-04-01 19:37:31</span><br><span class="hljs-comment"> * @LastEditors  : liu wei</span><br><span class="hljs-comment"> * @LastEditTime : 2022-04-02 12:33:10</span><br><span class="hljs-comment"> * @brief        : Do not edit</span><br><span class="hljs-comment"> * @FilePath     : \LED\Core\Src\mpuiic.c</span><br><span class="hljs-comment"> * @Github       : https://github.com/Blackerrr</span><br><span class="hljs-comment"> * @Coding       : utf-8</span><br><span class="hljs-comment"> */</span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&quot;mpuiic.h&quot;</span></span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: MPU IIC delay function</span><br><span class="hljs-comment"> * @param &#123;cnt&#125;  延时的us数</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Delay</span><span class="hljs-params">(u32 cnt)</span></span><br><span class="hljs-function"></span>&#123;<br>    delay_us(cnt);<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">IIC_SDA_OUT</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">// 使用寄存器操作比较迅速</span><br>    GPIOC-&gt;CRL &amp;= <span class="hljs-number">0XFFFFFF0F</span>;<br>    GPIOC-&gt;CRL |= <span class="hljs-number">3</span> &lt;&lt; <span class="hljs-number">4</span>;<br><br>    <span class="hljs-comment">// 使用库切换的话时间肯定比上面的慢， 但也是可以用的，优先选择使用寄存器切换</span><br>    <span class="hljs-comment">// GPIO_InitTypeDef GPIO_Initure;</span><br>    <span class="hljs-comment">// GPIO_Initure.Pin = GPIO_PIN_1;</span><br>    <span class="hljs-comment">// GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;</span><br>    <span class="hljs-comment">// GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;</span><br>    <span class="hljs-comment">// HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">IIC_SDA_IN</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">// 使用寄存器操作比较迅速</span><br>    GPIOC-&gt;CRL &amp;= <span class="hljs-number">0XFFFFFF0F</span>;<br>    GPIOC-&gt;CRL |= <span class="hljs-number">8</span> &lt;&lt; <span class="hljs-number">4</span>;<br><br>    <span class="hljs-comment">// 使用库切换的话时间肯定比上面的慢， 但也是可以用的，优先选择使用寄存器切换</span><br>    <span class="hljs-comment">// GPIO_InitTypeDef GPIO_Initure;</span><br>    <span class="hljs-comment">// GPIO_Initure.Pin = GPIO_PIN_1;</span><br>    <span class="hljs-comment">// GPIO_Initure.Mode = GPIO_MODE_INPUT;</span><br>    <span class="hljs-comment">// GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;</span><br>    <span class="hljs-comment">// HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);</span><br>&#125;<br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description:</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Init</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    GPIO_InitTypeDef GPIO_Initure;<br><br>    __HAL_RCC_GPIOC_CLK_ENABLE();<br>    GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1;<br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;<br>    <span class="hljs-comment">// GPIO_Initure.Pull = GPIO_PULLUP;     // 一般IIC设备SCL和SDA 接上拉电阻</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span><br>    GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;    <span class="hljs-comment">// 开漏输出模式</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;<br><br>    HAL_GPIO_Init(GPIOC, &amp;GPIO_Initure);<br><br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;    <span class="hljs-comment">// 总线空闲状态</span><br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description:  检测I2C总线设备，CPU向发送设备地址，然后读取设备应答来判断该设备是否存在</span><br><span class="hljs-comment"> * @param &#123;u8&#125; addr 设备的I2C总线地址</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Check_Device</span><span class="hljs-params">(u8 addr)</span></span><br><span class="hljs-function"></span>&#123;<br>    u8 res;<br>    MPU_IIC_Start();<br>    MPU_IIC_Send_Byte((addr &lt;&lt; <span class="hljs-number">1</span>) | <span class="hljs-number">0</span>);   <span class="hljs-comment">// 发送设备地址+写指令</span><br>    res = MPU_IIC_Wait_Ack();<br>    MPU_IIC_Stop();<br>    <span class="hljs-keyword">return</span> res;<br>&#125;<br><br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description:  Send IIC start signal</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Start</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;<br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SDA = <span class="hljs-number">0</span>;    <span class="hljs-comment">// START:when CLK is high,DATA change form high to low</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;    <span class="hljs-comment">// Clamp the I2C bus, ready to send or receive data</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br><br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: send iic stop signal</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Stop</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>    MPU_IIC_SDA = <span class="hljs-number">0</span>;  <span class="hljs-comment">//STOP:when CLK is high DATA change form low to high</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;  <span class="hljs-comment">// Send I2C bus end signal</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>&#125;<br><br><span class="hljs-comment">// Wait for the answer signal to arrive</span><br><span class="hljs-comment">// Return value: 1, fail</span><br><span class="hljs-comment">//               0, success</span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: 等待应答信号到来， Return value: 1, fail，0, success</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125; Return value: 1, fail，0, success</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Wait_Ack</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    u8 ucErrTime = <span class="hljs-number">0</span>;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_IN();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;<br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    <span class="hljs-keyword">while</span> (MPU_READ_SDA)   <span class="hljs-comment">// SDA 高表示非应答</span><br>    &#123;<br>        ucErrTime++;<br>        <span class="hljs-keyword">if</span> (ucErrTime &gt; <span class="hljs-number">250</span>)<br>        &#123;<br>            MPU_IIC_Stop();<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>        &#125;<br>    &#125;<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>; <span class="hljs-comment">// clock out 0</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: generate ACK response</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Ack</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();      <span class="hljs-comment">// 必须先将SCL置0,再置SDA为输出</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    MPU_IIC_SDA = <span class="hljs-number">0</span>;    <span class="hljs-comment">// master控制SDA为低表示应答</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;    <span class="hljs-comment">// master产生一个时钟</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;   <span class="hljs-comment">// master释放SDA总线</span><br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: No ACK response is generated</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_NAck</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span><br><span class="hljs-function"></span>&#123;<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    MPU_IIC_SDA = <span class="hljs-number">1</span>;   <span class="hljs-comment">// master控制SDA为高表示应答</span><br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>    MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: IIC发送一个字节</span><br><span class="hljs-comment"> * @param &#123;u8&#125; txd</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Send_Byte</span><span class="hljs-params">(u8 txd)</span></span><br><span class="hljs-function"></span>&#123;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    u8 t;<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;   <span class="hljs-comment">// Pull the clock low to start data transfer</span><br>    <span class="hljs-keyword">for</span> (t = <span class="hljs-number">0</span>; t &lt; <span class="hljs-number">8</span>; t++)<br>    &#123;<br>        MPU_IIC_SDA = (txd &amp; <span class="hljs-number">0x80</span>) &gt;&gt; <span class="hljs-number">7</span>;<br>        txd &lt;&lt;= <span class="hljs-number">1</span>;<br>        MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>        MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>        <span class="hljs-keyword">if</span> (t == <span class="hljs-number">7</span>)<br>            MPU_IIC_SDA = <span class="hljs-number">1</span>;   <span class="hljs-comment">// 释放总线</span><br>    &#125;<br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * @description: Read 1 byte, when ack=1, send ACK, when ack=0, send nACK</span><br><span class="hljs-comment"> * @param &#123;*&#125;</span><br><span class="hljs-comment"> * @return &#123;*&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Read_Byte</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> ack)</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">// 不管前面应答还是不应答。这边数据读取都不会受到影响</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_IN();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    u8 i, receive = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">8</span>; i++)<br>    &#123;<br>        MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>        MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>        receive &lt;&lt;= <span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">if</span> (MPU_READ_SDA)<br>            receive++;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-keyword">if</span> (!ack)<br>        MPU_IIC_NAck(); <span class="hljs-comment">// No ACK</span><br>    <span class="hljs-keyword">else</span><br>        MPU_IIC_Ack(); <span class="hljs-comment">// ACK</span><br>    <span class="hljs-keyword">return</span> receive;<br>&#125;<br><br><br></code></pre></td></tr></table></figure>
<ul>
<li>iic.h</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">/*</span><br><span class="hljs-comment"> * @Date         : 2022-04-01 19:39:42</span><br><span class="hljs-comment"> * @LastEditors  : liu wei</span><br><span class="hljs-comment"> * @LastEditTime : 2022-04-02 13:00:16</span><br><span class="hljs-comment"> * @brief        : Do not edit</span><br><span class="hljs-comment"> * @FilePath     : \LED\Core\Inc\mpuiic.h</span><br><span class="hljs-comment"> * @Github       : https://github.com/Blackerrr</span><br><span class="hljs-comment"> * @Coding       : utf-8</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">ifndef</span> __MPUIIC_H</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> __MPUIIC_H</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&quot;main.h&quot;</span></span><br><br><span class="hljs-comment">/********************************* MPUIIC ***************************************/</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MPU_IIC_SCL PCout(0) <span class="hljs-comment">//SCL</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MPU_IIC_SDA PCout(1) <span class="hljs-comment">//SDA</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MPU_READ_SDA PCin(1) <span class="hljs-comment">// input SDA</span></span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> USING_PP    0         <span class="hljs-comment">// 1 IIC 使用推挽   0  IIC 使用开漏</span></span><br><br><br><br><span class="hljs-comment">//IIC All function operations</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Init</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;                 <span class="hljs-comment">// Initialize the IO port of the IIC</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Start</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;                <span class="hljs-comment">// Send IIC start signal</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Stop</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;                 <span class="hljs-comment">// send IIC stop signal</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Send_Byte</span><span class="hljs-params">(u8 txd)</span></span>;          <span class="hljs-comment">// IIC sends a byte</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Read_Byte</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> ack)</span></span>; <span class="hljs-comment">// IIC reads a byte</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Wait_Ack</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;               <span class="hljs-comment">// IIC waits for ACK signal</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Ack</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;                  <span class="hljs-comment">// IIC sends ACK signal</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_NAck</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>;                 <span class="hljs-comment">// IIC does not send ACK signal</span><br><span class="hljs-function">u8 <span class="hljs-title">MPU_IIC_Check_Device</span><span class="hljs-params">(u8 addr)</span></span>;       <span class="hljs-comment">// 检查设备是否存在</span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br></code></pre></td></tr></table></figure>
<h2 id="stm32f4-iic调试记录">STM32F4 IIC调试记录</h2>
<ul>
<li><p>首先，上面在F1跑通的代码在F4上跑的话，就是丛机无应答。玄学问题，我现在也不知道为什么，不过我最后查看了正点原子的IIC代码，<del>发现里面的延时时间发生了很大的变换。（20220402）</del></p></li>
<li><p>不需要改变改变很多延时时间，延时一般2us足够。丛机无应答的问题有两个</p>
<ul>
<li><p>当使用推挽模式+切换输入输出，从机无应答，这个方案在F1上通的，原子的F4使用的也是这个方案，但我今天调试没通，暂时没找到原因。</p></li>
<li><p>主机发送一个字节的时序问题。下面代码中的第一个延时没加时，丛机无响应，加了之后就有了（在<strong>开漏输出</strong>模式下）。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">MPU_IIC_Send_Byte</span><span class="hljs-params">(u8 txd)</span></span><br><span class="hljs-function"></span>&#123;<br><span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> USING_PP</span><br>    IIC_SDA_OUT();<br><span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span><br>    u8 t;<br>    MPU_IIC_SCL = <span class="hljs-number">0</span>;   <span class="hljs-comment">// Pull the clock low to start data transfer</span><br>    <span class="hljs-keyword">for</span> (t = <span class="hljs-number">0</span>; t &lt; <span class="hljs-number">8</span>; t++)<br>    &#123;<br>        MPU_IIC_SDA = (txd &amp; <span class="hljs-number">0x80</span>) &gt;&gt; <span class="hljs-number">7</span>;<br>        txd &lt;&lt;= <span class="hljs-number">1</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">1</span>);      <span class="hljs-comment">// 这个延时是必须的，</span><br>        MPU_IIC_SCL = <span class="hljs-number">1</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>        MPU_IIC_SCL = <span class="hljs-number">0</span>;<br>        MPU_IIC_Delay(<span class="hljs-number">2</span>);<br>        <span class="hljs-keyword">if</span> (t == <span class="hljs-number">7</span>)<br>            MPU_IIC_SDA = <span class="hljs-number">1</span>;   <span class="hljs-comment">// 释放总线</span><br>    &#125;<br>&#125;<br><br><br></code></pre></td></tr></table></figure></li>
</ul></li>
<li><p>下次再遇到丛机无响应的问题，首先考虑IIC的发送数据时序是不是出了问题。</p></li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/STM32/">STM32</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">学习记录</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/04/09/kalman_filter/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Kalman 滤波记录</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/24/c_pointer_function/">
                        <span class="hidden-mobile">函数指针与回调函数</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"QsGrK96fVoTEvV5jcXID1kB4-gzGzoHsz","appKey":"Bb0EjzO9Ya2umqFfV2tep53O","path":"window.location.pathname","placeholder":null,"avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"https://qsgrk96f.lc-cn-n1-shared.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          Fluid.plugins.initFancyBox('#valine .vcontent img:not(.vemoji)');
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
